package com.hz.ruoyiequipment.equipment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hz.ruoyiequipment.data.domain.AgrEquipment;
import com.hz.ruoyiequipment.equipment.domain.AgrEquipmentFertigation;
import com.hz.ruoyiequipment.equipment.domain.AgrEquipmentSf;
import com.hz.ruoyiequipment.equipment.domain.AgrFertigationData;
import com.hz.ruoyiequipment.equipment.domain.vo.WafEquipment;
import com.hz.ruoyiequipment.equipment.mapper.WafEquipmentMapper;
import com.hz.ruoyiequipment.equipment.service.WafEquipmentService;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author pan
 * @date 2025/3/8 17:27
 * @description:  水肥一体机服务实现类
 * @version: 1.0
 */

@Service
public class WafEquipmentServiceImpl extends ServiceImpl<WafEquipmentMapper, WafEquipment>
        implements WafEquipmentService {

    @Autowired
    private  WafEquipmentMapper wafEquipmentMapper;
    @Autowired
    private SfPlotsdRedisService sfPlotsdRedisService;




    @Override
    public AjaxResult getWafEquipment(Integer equipmentStatus, String equipmentCode, String plotsName) {
        return AjaxResult.success(wafEquipmentMapper.getWafEquipment(equipmentStatus,equipmentCode,plotsName));
    }

    @Override
    public AjaxResult getWafDetail(Long equipmentId) {
        return  AjaxResult.success(wafEquipmentMapper.getWafDetail(equipmentId));
    }

    @Override
    @Transactional
    public AjaxResult insertWafEquipment(AgrEquipment agrEquipment, AgrEquipmentSf agrEquipmentSf) {
        if (agrEquipment == null || agrEquipmentSf == null) {
            return AjaxResult.error("新增水肥设备失败");
        }
        int rows = wafEquipmentMapper.insertEquipment(agrEquipment);
        if (rows < 0) {
            return AjaxResult.error("新增水肥设备失败");
        }
        long equipmentId = agrEquipment.getEquipmentId();
        agrEquipmentSf.setEquipmentId(equipmentId);
        rows = wafEquipmentMapper.insertWafEquipment(agrEquipmentSf);
        wafEquipmentMapper.insertAgrEquipmentFertigation(equipmentId);
        if (rows < 0) {
            return AjaxResult.error("新增水肥设备失败");
        }
        return AjaxResult.success("新增水肥设备成功");
    }

    @Override
    @Transactional
    public AjaxResult updateWafEquipment(WafEquipment wafEquipment, List<Integer> plotsIdList) {
        // 更新设备和水肥机相关数据
        wafEquipmentMapper.updateEquipment(wafEquipment);
        wafEquipmentMapper.updateEquipmentSf(wafEquipment);
        // 删除原有的设备与地块绑定关系
        wafEquipmentMapper.delEquipmentPlotsByEquipmentId(wafEquipment.getEquipmentId());
        // 插入新的绑定记录，直接遍历 List<Integer>
        if (plotsIdList != null && !plotsIdList.isEmpty()){
            for (Integer plotId : plotsIdList) {
                WafEquipment equipmentPlot = new WafEquipment();
                equipmentPlot.setEquipmentId(wafEquipment.getEquipmentId());
                equipmentPlot.setPlotsId(String.valueOf(plotId));
                wafEquipmentMapper.insertEquipmentPlots(equipmentPlot);
                //更新redis中地块灌溉状态
                sfPlotsdRedisService.updateThresholdRules(wafEquipment.getEquipmentId(), String.valueOf(plotId), 0);
            }
        }
        return AjaxResult.success("修改水肥设备成功");
    }

    @Override
    @Transactional
    public AjaxResult delWafEquipmentByEquipmentId(Long equipmentId) {
        wafEquipmentMapper.delEquipmentByEquipmentId(equipmentId);
        wafEquipmentMapper.delEquipmentSfByEquipmentId(equipmentId);
        wafEquipmentMapper.delEquipmentPlotsByEquipmentId(equipmentId);
        wafEquipmentMapper.delAgrEquipmentFertigationByEquipmentId(equipmentId);
        sfPlotsdRedisService.deleteThresholdRules(equipmentId, null);
        return AjaxResult.success("删除水肥设备成功");
    }

    @Override
    @Transactional
    public AjaxResult updateAgrEquipmentFertigation(AgrEquipmentFertigation agrEquipmentFertigation) {
        Integer mainPump = wafEquipmentMapper.getAgrEquipmentFertigation(agrEquipmentFertigation.getEquipmentId()).getMainPump();
        if (mainPump == 1) {
            return AjaxResult.error("主泵正在运行，无法修改");
        }else {
            wafEquipmentMapper.updateAgrEquipmentFertigation(agrEquipmentFertigation);
            List<Long> list=agrEquipmentFertigation.getPlotsId();
            for (Long plotsId : list) {
                //更新redis中地块灌溉状态
                sfPlotsdRedisService.updateThresholdRules(agrEquipmentFertigation.getEquipmentId(), String.valueOf(plotsId), 1);
            }
            return AjaxResult.success();
        }
    }

    @Override
    public AjaxResult getWafEquipmentID(Long equipmentId) {
        return AjaxResult.success(wafEquipmentMapper.getWafEquipmentID(equipmentId));
    }

    @Override
    @Transactional
    public AjaxResult updateSfStatus(AgrEquipmentFertigation agrEquipmentFertigation ) {
        agrEquipmentFertigation.setMainPump(0);
        Map<String, Object> params = new HashMap<>();
        params.put("equipmentId", agrEquipmentFertigation.getEquipmentId());
        params.put("plotsId", agrEquipmentFertigation.getPlotsId());
        params.put("waterUse", agrEquipmentFertigation.getWaterInjection());
        params.put("fertUse", agrEquipmentFertigation.getFertSolution());
        params.put("inletPressure", agrEquipmentFertigation.getInletPressure());
        params.put("instantWater", agrEquipmentFertigation.getInstantWater());
        params.put("instantFerti", agrEquipmentFertigation.getInstantFerti());
        wafEquipmentMapper.insertAgrFertigationDataBatch(params);
        AjaxResult ajaxResult =AjaxResult.success(wafEquipmentMapper.updateSfStatus(agrEquipmentFertigation));
        List<Long> list=agrEquipmentFertigation.getPlotsId();
        for (Long plotsId : list) {
            //更新redis中地块灌溉状态
            sfPlotsdRedisService.updateThresholdRules(agrEquipmentFertigation.getEquipmentId(), String.valueOf(plotsId), 0);
        }

        return ajaxResult;
    }

    @Override
    public List<AgrFertigationData> getAgrFertigationData(Long equipmentId) {
        return wafEquipmentMapper.getAgrFertigationData(equipmentId);
    }

    @Override
    public AjaxResult getAgrEquipmentFertigation(Long equipmentId) {
        return AjaxResult.success(wafEquipmentMapper.getAgrEquipmentFertigation(equipmentId));
    }

    @Override
    public Integer getSfStatusByEquipmentIdInner(Long equipmentId) {
        return wafEquipmentMapper.getSfStatusByEquipmentIdInner(equipmentId);
    }
}
